/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.IndexImg;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.model.StoreCoupon;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.admin.util.SecurityUtils;
import com.yami.shop.service.IndexImgService;
import com.yami.shop.service.ProductService;
import com.yami.shop.service.StoreCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import com.yami.shop.common.response.ServerResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author lgh on 2018/11/26.
 */
@RestController
@RequestMapping("/myactives/coupon")
public class CouponController {

    @Autowired
    private StoreCouponService storeCouponService;

    @Autowired
    private ProductService productService;


    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('myactives:coupon:page')")
    public ServerResponseEntity<IPage<StoreCoupon>> page(StoreCoupon storeCoupon, PageParam<StoreCoupon> page) {
        IPage<StoreCoupon> storeCouponPage = storeCouponService.page(page,
                new LambdaQueryWrapper<StoreCoupon>()
                        .eq(storeCoupon.getStatus() != null, StoreCoupon::getStatus, storeCoupon.getStatus())
                        .orderByAsc(StoreCoupon::getId));
        return ServerResponseEntity.success(storeCouponPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{imgId}")
    @PreAuthorize("@pms.hasPermission('myactives:coupon:info')")
    public ServerResponseEntity<StoreCoupon> info(@PathVariable("imgId") Long imgId) {
        Long shopId = SecurityUtils.getSysUser().getShopId();
        StoreCoupon storeCoupon = storeCouponService.getOne(new LambdaQueryWrapper<StoreCoupon>().eq(StoreCoupon::getId, imgId));
//
//        if (Objects.nonNull(indexImg.getRelation())) {
//            Product product = productService.getProductByProdId(indexImg.getRelation());
//            indexImg.setPic(product.getPic());
//            indexImg.setProdName(product.getProdName());
//        }
        return ServerResponseEntity.success(storeCoupon);
    }

    /**
     * 保存
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('myactives:coupon:save')")
    public ServerResponseEntity<Void> save(@RequestBody @Valid StoreCoupon storeCoupon) {
        Long shopId = SecurityUtils.getSysUser().getShopId();
//        indexImg.setShopId(shopId);
//        indexImg.setUploadTime(new Date());
//        checkProdStatus(indexImg);
        storeCoupon.setCreateTime(LocalDateTime.now());
        storeCouponService.save(storeCoupon);
        //indexImgService.removeIndexImgCache();
        return ServerResponseEntity.success();
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('myactives:coupon:update')")
    public ServerResponseEntity<Void> update(@RequestBody @Valid StoreCoupon storeCoupon) {
        //checkProdStatus(indexImg);
        storeCouponService.saveOrUpdate(storeCoupon);
        //indexImgService.removeIndexImgCache();
        return ServerResponseEntity.success();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('myactives:coupon:delete')")
    public ServerResponseEntity<Void> delete(@RequestBody Long[] ids) {
        //storeCouponService.deleteStoreCouponByIds(ids);
        List<Long> longs = Arrays.asList(ids);
        storeCouponService.removeBatchByIds(longs);

//        indexImgService.removeIndexImgCache();
        return ServerResponseEntity.success();
    }

//    private void checkProdStatus(IndexImg indexImg) {
//        if (!Objects.equals(indexImg.getType(), 0)) {
//            return;
//        }
//        if (Objects.isNull(indexImg.getRelation())) {
//            throw new YamiShopBindException("请选择商品");
//        }
//        Product product = productService.getById(indexImg.getRelation());
//        if (Objects.isNull(product)) {
//            throw new YamiShopBindException("商品信息不存在");
//        }
//        if (!Objects.equals(product.getStatus(), 1)) {
//            throw new YamiShopBindException("该商品未上架，请选择别的商品");
//        }
//    }
}
